package com.scraper.service.impl;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import com.scraper.exception.InvalidContentException;
import com.scraper.model.SaleObject;
import com.scraper.realestate.BaseSiteScraper;
import com.scraper.service.CityManager;
import com.scraper.service.RegionManager;
import com.scraper.service.SaleObjectManager;
import com.scraper.service.SiteEstkvartiraScraper;

public class SiteEstkvartiraScraperImpl extends BaseSiteScraper implements	SiteEstkvartiraScraper {

	private static final long serialVersionUID = -6537175363997536968L;

	private CityManager cityManager;

	private RegionManager regionManager;
	
	private SaleObjectManager saleObjectManager;
	
	public List<String> findNavigationChain(String pageURL) throws IOException,
			InvalidContentException, XPatherException {
		List<String> navigationList = new ArrayList<String>();
		String content = readRemoteSite(pageURL);
		if (StringUtils.isBlank(content)) {
			throw new InvalidContentException();
		}
		
		TagNode root = cleanUpToDHTML(content);
		
		if (root == null) {
			throw new IOException("Can't cleanup");
		}
		
		Object[] items = root.evaluateXPath("//p[@class='pPagination']/a[@href]");
		TagNode pageCountNode = (TagNode) items[0];
		String pageLink = pageCountNode.getAttributeByName("href");
		Integer totalPageCount = Integer.parseInt(pageLink.substring(pageLink.lastIndexOf("=")+1, pageLink.length()));
//		String pageSuffics = pageLink.replaceAll("amp;", "");
		for (int i = 1; i <= totalPageCount; i++) {
			String page = pageURL + "&page="+i;//pageSuffics.substring(0, pageSuffics.length()-1)+i;
			navigationList.add(page);
		}
		return navigationList;
	}

	public CityManager getCityManager() {
		return cityManager;
	}

	public RegionManager getRegionManager() {
		return regionManager;
	}

	public SaleObjectManager getSaleObjectManager() {
		return saleObjectManager;
	}

	public void run(String siteURL) throws IOException,
			InvalidContentException, XPatherException, ParseException {
		List<String> navigationChain = findNavigationChain(siteURL);
		for (String pageURL : navigationChain) {
			scrapRemoteFeed(pageURL);
		}
	}

	@Override
	public List<SaleObject> scrap(String content, boolean isLocal)
			throws InvalidContentException, IOException, XPatherException,
			ParseException {
		List<SaleObject> saleObjectList = new ArrayList<SaleObject>();
		if (StringUtils.isBlank(content)) {
			throw new InvalidContentException();
		}

		TagNode root = cleanUpToDHTML(content);

		if (root == null) {
			throw new IOException("Can't cleanup");
		}

		return saleObjectList;
	}

	public void setCityManager(CityManager cityManager) {
		this.cityManager = cityManager;
	}

	public void setRegionManager(RegionManager regionManager) {
		this.regionManager = regionManager;
	}

	public void setSaleObjectManager(SaleObjectManager saleObjectManager) {
		this.saleObjectManager = saleObjectManager;
	}

}
